package presentation;

import businessEntities.ColumnsInformationContainer;
import businessEntities.TestQueryInformation;
import businessLogic.BLQueryTester;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import presentation.entities.DialogCreator;

public class AddFilterDialog extends javax.swing.JDialog {

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addFilter_panel = new javax.swing.JPanel();
        whereSentence_scrollPane = new javax.swing.JScrollPane();
        whereSentence_textField = new javax.swing.JTextField();
        value_textField = new javax.swing.JTextField();
        add_button = new javax.swing.JButton();
        test_button = new javax.swing.JButton();
        or_button = new javax.swing.JButton();
        and_button = new javax.swing.JButton();
        saveFilter_button = new javax.swing.JButton();
        cancel_button = new javax.swing.JButton();
        clear_button = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        typeTextField_scrollPane = new javax.swing.JScrollPane();
        type_textFiled = new javax.swing.JTextField();
        operatorTextField_scrollPane = new javax.swing.JScrollPane();
        operator_textField = new javax.swing.JTextField();
        columns_comboBox = new javax.swing.JComboBox();
        operators_comboBox = new javax.swing.JComboBox();
        jSeparator3 = new javax.swing.JSeparator();
        leftBracket_button = new javax.swing.JButton();
        rightBracket_button = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add New Filter");
        setModal(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        addFilter_panel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        whereSentence_scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        whereSentence_textField.setEditable(false);
        whereSentence_textField.setFont(new java.awt.Font("Microsoft YaHei", 1, 12));
        whereSentence_textField.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        whereSentence_textField.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        whereSentence_textField.setFocusable(false);
        whereSentence_textField.setRequestFocusEnabled(false);
        whereSentence_scrollPane.setViewportView(whereSentence_textField);

        value_textField.setEditable(false);
        value_textField.setFont(new java.awt.Font("Microsoft YaHei", 1, 14));
        value_textField.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        value_textField.setFocusable(false);

        add_button.setFont(new java.awt.Font("Segoe Print", 1, 14));
        add_button.setText("Add");
        add_button.setEnabled(false);
        add_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                add_buttonActionPerformed(evt);
            }
        });

        test_button.setFont(new java.awt.Font("Segoe Print", 1, 14));
        test_button.setText("Test Filter");
        test_button.setEnabled(false);
        test_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                test_buttonActionPerformed(evt);
            }
        });

        or_button.setFont(new java.awt.Font("Segoe Print", 1, 14));
        or_button.setText("OR");
        or_button.setEnabled(false);
        or_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                or_buttonActionPerformed(evt);
            }
        });

        and_button.setFont(new java.awt.Font("Segoe Print", 1, 14));
        and_button.setText("AND");
        and_button.setEnabled(false);
        and_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                and_buttonActionPerformed(evt);
            }
        });

        saveFilter_button.setFont(new java.awt.Font("Segoe Print", 1, 14)); // NOI18N
        saveFilter_button.setText("Save (Add Filter)");
        saveFilter_button.setEnabled(false);
        saveFilter_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveFilter_buttonActionPerformed(evt);
            }
        });

        cancel_button.setFont(new java.awt.Font("Segoe Print", 1, 14));
        cancel_button.setText("Cancel");
        cancel_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_buttonActionPerformed(evt);
            }
        });

        clear_button.setFont(new java.awt.Font("Segoe Print", 1, 14));
        clear_button.setText("Clear");
        clear_button.setEnabled(false);
        clear_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clear_buttonActionPerformed(evt);
            }
        });

        jSeparator2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        typeTextField_scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        type_textFiled.setEditable(false);
        type_textFiled.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 11));
        type_textFiled.setFocusable(false);
        typeTextField_scrollPane.setViewportView(type_textFiled);

        operatorTextField_scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        operatorTextField_scrollPane.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 11));

        operator_textField.setEditable(false);
        operator_textField.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 12));
        operator_textField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        operator_textField.setFocusable(false);
        operatorTextField_scrollPane.setViewportView(operator_textField);

        columns_comboBox.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 12));

        operators_comboBox.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 12));

        jSeparator3.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        leftBracket_button.setFont(new java.awt.Font("Segoe Print", 1, 14));
        leftBracket_button.setText("(");
        leftBracket_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                leftBracket_buttonActionPerformed(evt);
            }
        });

        rightBracket_button.setFont(new java.awt.Font("Segoe Print", 1, 14));
        rightBracket_button.setText(")");
        rightBracket_button.setEnabled(false);
        rightBracket_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rightBracket_buttonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout addFilter_panelLayout = new org.jdesktop.layout.GroupLayout(addFilter_panel);
        addFilter_panel.setLayout(addFilter_panelLayout);
        addFilter_panelLayout.setHorizontalGroup(
            addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, addFilter_panelLayout.createSequentialGroup()
                .addContainerGap()
                .add(addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, addFilter_panelLayout.createSequentialGroup()
                        .add(addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(columns_comboBox, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(typeTextField_scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE))
                        .add(18, 18, 18)
                        .add(addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(operators_comboBox, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(operatorTextField_scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
                        .add(18, 18, 18)
                        .add(addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(add_button, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                            .add(value_textField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)))
                    .add(jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 644, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, addFilter_panelLayout.createSequentialGroup()
                        .add(whereSentence_scrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 420, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(or_button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 80, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(addFilter_panelLayout.createSequentialGroup()
                                .add(leftBracket_button)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(rightBracket_button)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 52, Short.MAX_VALUE)
                        .add(and_button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 80, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jSeparator3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 644, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, addFilter_panelLayout.createSequentialGroup()
                        .add(saveFilter_button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 153, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(cancel_button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 153, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(clear_button, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(test_button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 153, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        addFilter_panelLayout.setVerticalGroup(
            addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, addFilter_panelLayout.createSequentialGroup()
                .addContainerGap()
                .add(addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(addFilter_panelLayout.createSequentialGroup()
                        .add(addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(add_button, 0, 0, Short.MAX_VALUE)
                            .add(columns_comboBox))
                        .add(17, 17, 17))
                    .add(addFilter_panelLayout.createSequentialGroup()
                        .add(operators_comboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
                .add(addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(operatorTextField_scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, typeTextField_scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, value_textField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE))
                .add(6, 6, 6)
                .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(addFilter_panelLayout.createSequentialGroup()
                        .add(addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(and_button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 27, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(or_button, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 27, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 10, Short.MAX_VALUE)
                        .add(addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(leftBracket_button)
                            .add(rightBracket_button)))
                    .add(whereSentence_scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addFilter_panelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(saveFilter_button, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(clear_button, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(test_button, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                    .add(cancel_button))
                .addContainerGap())
        );

        getContentPane().add(addFilter_panel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void cancel_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_buttonActionPerformed
        isReady = false;
        dispose();
    }//GEN-LAST:event_cancel_buttonActionPerformed

    private void clear_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clear_buttonActionPerformed
        whereSentence_textField.setText("");
        clear_button.setEnabled(false);
        balanceCounter = 0;
        this.changeToOrAnd(false);
        saveFilter_button.setEnabled(false);
    }//GEN-LAST:event_clear_buttonActionPerformed

    private void add_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_add_buttonActionPerformed

        String value = value_textField.getText();
        String operator = operator_textField.getText();
        String column = (String)columsComboModel.getSelectedItem();
        String currentFilter = whereSentence_textField.getText().trim();
        boolean enableClearBtn = true;
        
        if(!currentFilter.equals("")) {
            currentFilter += " ";
            enableClearBtn = false;
        }
        
        if(operator.equals("IS NULL") || operator.equals("IS NOT NULL")){
            whereSentence_textField.setText(currentFilter + column + " " + operator);
            changeToOrAnd(true);
            if(enableClearBtn) clear_button.setEnabled(true);
        } else {
            whereSentence_textField.setText(currentFilter + column + " " + operator + " '"+ value +"'");
            changeToOrAnd(true);
            if(enableClearBtn) clear_button.setEnabled(true);
        }
    }//GEN-LAST:event_add_buttonActionPerformed

    private void or_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_or_buttonActionPerformed
        String currentText = whereSentence_textField.getText();
        whereSentence_textField.setText(currentText + " OR");
        changeToOrAnd(false);
        saveFilter_button.setEnabled(false);
    }//GEN-LAST:event_or_buttonActionPerformed

    private void and_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_and_buttonActionPerformed
       String currentText = whereSentence_textField.getText();
       whereSentence_textField.setText(currentText + " AND");
       changeToOrAnd(false);        // TODO add your handling code here:
       saveFilter_button.setEnabled(false);
    }//GEN-LAST:event_and_buttonActionPerformed

    private void saveFilter_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveFilter_buttonActionPerformed
        isReady = true;
        this.dispose();
    }//GEN-LAST:event_saveFilter_buttonActionPerformed

    private void test_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_test_buttonActionPerformed
        String currentFilter = whereSentence_textField.getText().trim();
        String where;
        if (previousWhere.equals(""))  where = currentFilter;
        else where = "(" + previousWhere + ")" + " AND (" +  currentFilter +")";

        BLQueryTester tester = new BLQueryTester();
        TestQueryInformation information = tester.testWhereQuery(columsDataContainer.getDatabaseName(), columsDataContainer.getTableName(), where);
        if(information.testWasPassed()){
            DialogCreator.showMessage(this, "Test Passed", 
                                        "SQL query : "+information.getQuery()
                                        +"\nResult : Test was successfully passed.", 
                                        JOptionPane.INFORMATION_MESSAGE);
        }
        else {
            DialogCreator.showMessage(this, "Test Failed",
                                        "SQL query : "+information.getQuery() +
                                        "\nReuslt :Test was failed." +
                                        "\nError message:\n" + information.getErrorMessage(), 
                                        JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_test_buttonActionPerformed

    private void leftBracket_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_leftBracket_buttonActionPerformed
       
        String currentText = whereSentence_textField.getText();
        if(currentText.length() > 0)
            whereSentence_textField.setText(currentText + " (");
        else
            whereSentence_textField.setText(currentText + "(");
        balanceCounter++;
        leftBracket_button.setEnabled(true);
       
    }//GEN-LAST:event_leftBracket_buttonActionPerformed

    private void rightBracket_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rightBracket_buttonActionPerformed
        String currentText = whereSentence_textField.getText();
        whereSentence_textField.setText(currentText + ")");
        balanceCounter--;
        if(balanceCounter == 0){
            rightBracket_button.setEnabled(false);
            saveFilter_button.setEnabled(true);
        } else{
            rightBracket_button.setEnabled(true);
            saveFilter_button.setEnabled(false);
        }
    }//GEN-LAST:event_rightBracket_buttonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel addFilter_panel;
    private javax.swing.JButton add_button;
    private javax.swing.JButton and_button;
    private javax.swing.JButton cancel_button;
    private javax.swing.JButton clear_button;
    private javax.swing.JComboBox columns_comboBox;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JButton leftBracket_button;
    private javax.swing.JScrollPane operatorTextField_scrollPane;
    private javax.swing.JTextField operator_textField;
    private javax.swing.JComboBox operators_comboBox;
    private javax.swing.JButton or_button;
    private javax.swing.JButton rightBracket_button;
    private javax.swing.JButton saveFilter_button;
    private javax.swing.JButton test_button;
    private javax.swing.JScrollPane typeTextField_scrollPane;
    private javax.swing.JTextField type_textFiled;
    private javax.swing.JTextField value_textField;
    private javax.swing.JScrollPane whereSentence_scrollPane;
    private javax.swing.JTextField whereSentence_textField;
    // End of variables declaration//GEN-END:variables
    private boolean isReady = false;
    private ColumnsInformationContainer columsDataContainer;
    private DefaultComboBoxModel<String> columsComboModel;
    private final String OPER_DEF_COMBO = "Operator :";
    private final String COL_DEF_COMBO = "Columns :";
    private final String previousWhere;
    private int balanceCounter = 0;

    /** Creates new form AddFilterDialog */
    public AddFilterDialog(java.awt.Frame parent, ColumnsInformationContainer columnsData, String previousWhere) {
        super(parent);
        initComponents();
        this.previousWhere = previousWhere;
        this.setLocationRelativeTo(parent);

        columsDataContainer = columnsData;

        String[] colums = columsDataContainer.getColumsCopy().toArray(new String[0]);
        columsComboModel = new DefaultComboBoxModel<>(colums);
        columsComboModel.setSelectedItem("Select Column : ");
        columns_comboBox.setModel(columsComboModel);
        initComboListner();

        Image icon = Toolkit.getDefaultToolkit().getImage(PresentationConfig.newFilterIconPath);
        this.setIconImage(icon);

    }
    
    public AddFilterDialog(TableFilteringScreen parent, ColumnsInformationContainer columnsData, String previousWhere, String currentFilter) {
        this(parent, columnsData, previousWhere);
        whereSentence_textField.setText(currentFilter);
        changeToOrAnd(true);
        clear_button.setEnabled(true);
    }

    public String getWhere() {
        if (isReady) {
            return whereSentence_textField.getText();
        } else {
            return null;
        }
    }

    private void initComboListner() {
        columns_comboBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Object value = columsComboModel.getSelectedItem();
                if (value != null && !value.equals("") && !value.equals(COL_DEF_COMBO)) {
                    String selectedColumn = (String) value;
                    type_textFiled.setText(columsDataContainer.getColumnType(selectedColumn) + " , "
                            + " Nullable - " + columsDataContainer.isNullableColumn(selectedColumn));

                    String[] operators = columsDataContainer.getOperators(selectedColumn);
                    DefaultComboBoxModel<String> operatorsModel = new DefaultComboBoxModel<>(operators);
                    operatorsModel.setSelectedItem(OPER_DEF_COMBO);
                    operators_comboBox.setModel(operatorsModel);
                    operator_textField.setText("");
                    enableValueField(false);
                    add_button.setEnabled(false);
                } else {
                    type_textFiled.setText("");
                }
            }
        });

        operators_comboBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Object value = operators_comboBox.getSelectedItem();
                if (value != null && !value.equals("") && !value.equals(OPER_DEF_COMBO)) {
                    operator_textField.setText((String) value);
                    add_button.setEnabled(true);
                    if (value.equals("IS NULL") || value.equals("IS NOT NULL")) {
                        enableValueField(false);
                    } else {
                        enableValueField(true);
                    }
                } else {
                    operator_textField.setText("");
                    enableValueField(true);
                    add_button.setEnabled(false);
                }
            }
        });
    }

    private void enableValueField(boolean b) {
        if (!b) {
            value_textField.setText("");
        }
        value_textField.setEditable(b);
        value_textField.setFocusable(b);
    }

    private void changeToOrAnd(boolean b) {
        if (b) {
            this.columsComboModel.setSelectedItem(COL_DEF_COMBO);
            this.columns_comboBox.setEnabled(false);
            type_textFiled.setText("");
            this.operators_comboBox.setModel(new DefaultComboBoxModel());
            operators_comboBox.setEnabled(false);
            operator_textField.setText("");
            add_button.setEnabled(false);
            enableValueField(false);
            and_button.setEnabled(true);
            or_button.setEnabled(true);
            if(balanceCounter == 0){
                saveFilter_button.setEnabled(true);
            } else{
                saveFilter_button.setEnabled(false);
            }
            test_button.setEnabled(true);
            if (balanceCounter > 0) {
                rightBracket_button.setEnabled(true);
            }
            leftBracket_button.setEnabled(false);
        } else {
            this.columsComboModel.setSelectedItem(COL_DEF_COMBO);
            this.columns_comboBox.setEnabled(true);
            type_textFiled.setText("");
            this.operators_comboBox.setModel(new DefaultComboBoxModel());
            operators_comboBox.setEnabled(true);
            operator_textField.setText("");
            add_button.setEnabled(false);
            enableValueField(false);
            and_button.setEnabled(false);
            or_button.setEnabled(false);
            if(balanceCounter == 0){
                saveFilter_button.setEnabled(true);
            } else{
                saveFilter_button.setEnabled(false);
            }
            test_button.setEnabled(false);
            rightBracket_button.setEnabled(false);
            leftBracket_button.setEnabled(true);
        }
    }

}

